package com.dsdl.chatapp.module.main.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.dsdl.chatapp.R;
import com.dsdl.chatapp.data.ChatList;
import com.dsdl.chatapp.data.ChatMsg;

import java.util.List;

/**
 * Created by zzg on 2016/8/13.
 * 聊天列表适配器
 */
public class ChatListAdapter extends RecyclerView.Adapter<ChatListAdapter.ViewHolder>{
    private List<ChatList> mList;
    private Context mContext;
    private LayoutInflater mInflater;

    public ChatListAdapter(Context context, List<ChatList> list){
        this.mContext = context;
        this.mList = list;
        this.mInflater = LayoutInflater.from(context);
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = mInflater.inflate(R.layout.item_chat_list,parent,false);
        ViewHolder viewHolder = new ViewHolder(view);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        holder.tvUserName.setText(mList.get(position).getTo());
        List<ChatMsg> messages = mList.get(position).getChatMsgs();
        if (messages != null && messages.size() > 0){
           String messageStr =  messages.get(messages.size() - 1).getContent();
            holder.tvMessage.setText(messageStr);
        }
    }

    @Override
    public int getItemCount() {
        return mList.size();
    }

    static class ViewHolder extends RecyclerView.ViewHolder{
        ImageView ivPhoto;
        TextView tvUserName;
        TextView tvMessage;

        public ViewHolder(View itemView) {
            super(itemView);
            ivPhoto = (ImageView) itemView.findViewById(R.id.iv_chat_list_photo);
            tvUserName = (TextView) itemView.findViewById(R.id.tv_chat_list_username);
            tvMessage = (TextView) itemView.findViewById(R.id.tv_chat_list_message);
        }
    }
}
